
#include <RTL.h>
#include <stdio.h>
#include "stm32f10x.h"
#include "SD_File.h"

#define __UART_DRV_C__
#include "uart_drv.h"

void Uart_Hint(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;						

  /* Enable USART1 clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);

  USART_DeInit(USART1);	

  /* Configure USARTx_Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;//| GPIO_Pin_2 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USARTx_Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;//| GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USARTx configuration ------------------------------------------------------*/

  /* USART1 configured as follow:
        - BaudRate = 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the middle
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */
  USART_InitStructure.USART_BaudRate = 115200;//115200;//921600;//19200;//1200;//115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  /* Configure the USARTx */
  USART_Init(USART1, &USART_InitStructure);
  //USART_Init(USART2, &USART_InitStructure);

  /* Enable the USART Receive interrupt: this interrupt is generated when the
   USART1 receive data register is not empty */
  //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

  #if USE_TX_BUF
  UartTxCon=0;
  UartTxAddr=0;
  #endif
  //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

  /* Enable the USARTx */
  USART_Cmd(USART1, ENABLE);
  //USART_Cmd(USART2, ENABLE);
}


int sendchar (int iChar)
{
	//if(stream == &(__stdout))
	{
		while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
		if(iChar == '\n')
		{	
			USART1->DR = '\r';
			while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
		}
		USART1->DR = iChar;
		while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
	}
	//else		fwrite(&iChar,1,1,stream);
	return iChar;
}
/*
int fgetc (
    FILE *stream)
	*/

int getkey (void) {
  while (!(USART1->SR & USART_FLAG_RXNE));
  return ((int)(USART1->DR & 0x1FF));
}

BOOL getline (char *lp, U32 n) {
   U32 cnt = 0;
   char c;

   do {
      c = getkey ();
      switch (c) {
         case CNTLQ:                       /* ignore Control S/Q             */
         case CNTLS:
            break;;
         case BACKSPACE:
         case DEL:
            if (cnt == 0) {
               break;
            }
            cnt--;                         /* decrement count                */
            lp--;                          /* and line pointer               */
            putchar (0x08);                /* echo backspace                 */
            putchar (' ');
            putchar (0x08);
            fflush (stdout);
            break;
         case ESC:
            *lp = 0;                       /* ESC - stop editing line        */
            return (__FALSE);
         case CR:                          /* CR - done, stop editing line   */
            *lp = c;
            lp++;                          /* increment line pointer         */
            cnt++;                         /* and count                      */
            c = LF;
         default:
            putchar (*lp = c);             /* echo and store character       */
            fflush (stdout);
            lp++;                          /* increment line pointer         */
            cnt++;                         /* and count                      */
            break;
      }
   } while (cnt < n - 2  &&  c != LF);     /* check limit and CR             */
   *lp = 0;                                /* mark end of string             */
   return (__TRUE);
}


//end file

